import { saveNormalStudent, getMajorTree } from '../../api/user'
import { listByKind, findColumnList } from '../../api/base'

Page({
  data: {
    pageHeight: 0,
    navHeight: 0,
    isDisabled: false,

    majorList: [],
    showMajor: false,
    selMajors: [],
    majorIndex: 0,

    phaseList: [],
    showPhase: false,
    selPhases: [],

    subjectList: [],
    showSubject: false,
    selSubjects: [],

    goodAtList: [],
    showGoodAt: false,
    selGoodAt: [],

    focusList: [],
    showFocus: false,
    selFocus: [],

    form: {
      focus: [],
      goodAtId: [],
      majorId: [],
      phaseId: [], // 学段
      subjectId: [] // 学科
    }
  },

  onLoad: function (options) {
    this.setPageHeight();
    this.init();
  },

  onShow() {
    let query = wx.createSelectorQuery().in(this);
    query.select('.info-nav-wrap').boundingClientRect();
    query.exec((res) => {
      this.setData({
        navHeight: res[0].height
      })
    });
  },

  back() {
    wx.navigateBack({ delta: 1 })
  },

  setPageHeight() {
    let windowHeight
    wx.getSystemInfo({
      success: function(res) {
        windowHeight = res.windowHeight
      }
    });
    let query = wx.createSelectorQuery().in(this);
    query.select('.info-nav-wrap').boundingClientRect();
    query.exec((res) => {
      this.setData({
        pageHeight: windowHeight - res[0].height
      })
    });
  },

  init() {
    this.getMajorList()
    listByKind('stage').then(res => {
      if (res.length) {
        res.forEach(r => {
          r.isCheck = false
        })
      }
      this.setData({
        phaseList: res
      })
    })
    listByKind('subject').then(res => {
      if (res.length) {
        res.forEach(r => {
          r.isCheck = false
        })
      }
      this.setData({
        subjectList: res
      })
    })
  },

  /** 选择专业 start **/
  getMajorList() {
    getMajorTree({
      depth: 1
    }).then(res => {
      if (res.length) {
        res.forEach(r => {
          r.text = r.name
          if (r.children) {
            r.children.forEach(c => {
              if (c.children.length) {
                c.children.forEach(major => {
                  major.isCheck = false
                })
              }
            })
          }
        })
      }
      this.setData({
        majorList: res
      })
    })
  },

  selectMajor() {
    this.setData({showMajor: true})
  },

  cancelSelectMajor() {
    this.setData({showMajor: false})
  },

  selectMajorNav({ detail = {} }){
    this.setData({
      majorIndex: detail.index || 0,
    });
  },

  checkMajor(e) {
    const item = e.currentTarget.dataset.item
    const index = e.currentTarget.dataset.index
    const supIndex = e.currentTarget.dataset.supindex
    const majorIndex = this.data.majorIndex
    item.isCheck = !item.isCheck
    this.data.majorList[majorIndex].children[supIndex].children[index].isCheck = item.isCheck
    this.setData({
      majorIndex: majorIndex,
      majorList: this.data.majorList,
    })
  },

  confirmSelectMajor() {
    this.data.selMajors = []
    this.data.form.majorId = []
    this.data.majorList.forEach(list => {
      if (list.children.length) {
        list.children.forEach(c => {
          if (c.children.length) {
            c.children.forEach(major => {
              if (major.isCheck) {
                this.data.selMajors.push(major.name)
                this.data.form.majorId.push(major.id)
              }
            })
          }
        })
      }
    })
    this.setData({
      showMajor: false,
      selMajors: this.data.selMajors,
      'form.majorId': this.data.form.majorId,

    })
  },

  /** 选择专业 end **/

  /** 选择科目 start **/
  selectSubject() {
    this.setData({showSubject: true})
  },

  cancelSelectSubject() {
    this.setData({showSubject: false})
  },

  checkSubject(e) {
    const item = e.currentTarget.dataset.item
    const index = e.currentTarget.dataset.index
    item.isCheck = !item.isCheck
    this.data.subjectList[index].isCheck = item.isCheck
    this.setData({
      subjectList: this.data.subjectList
    })
  },

  confirmSelectSubject() {
    this.data.selSubjects = []
    this.data.form.subjectId = []
    this.data.subjectList.forEach(subject => {
      if (subject.isCheck) {
        this.data.selSubjects.push(subject.name)
        this.data.form.subjectId.push(subject.code)
      }
    })
    this.setData({
      showSubject: false,
      selSubjects: this.data.selSubjects,
      'form.subjectId': this.data.form.subjectId,
      goodAtList: [],
      focusList: []
    })
  },
  /** 选择科目 end **/

  /** 选择目标学段 start **/
  selectPhase() {
    this.setData({showPhase: true})
  },

  cancelSelectPhase() {
    this.setData({showPhase: false})
  },

  checkPhase(e) {
    const item = e.currentTarget.dataset.item
    const index = e.currentTarget.dataset.index
    item.isCheck = !item.isCheck
    this.data.phaseList[index].isCheck = item.isCheck
    this.setData({
      phaseList: this.data.phaseList
    })
  },

  confirmSelectPhase() {
    this.data.selPhases = []
    this.data.form.phaseId = []
    this.data.phaseList.forEach(phase => {
      if (phase.isCheck) {
        this.data.selPhases.push(phase.name)
        this.data.form.phaseId.push(phase.code)
      }
    })
    this.setData({
      showPhase: false,
      selPhases: this.data.selPhases,
      'form.phaseId': this.data.form.phaseId,
      goodAtList: [],
      focusList: []
    })
  },
  /** 选择目标学段 end **/

  getFindColumnList() {
    if (!this.data.goodAtList.length || !this.data.focusList.length) {
      findColumnList({
        stageList: this.data.form.phaseId,
        subjectList: this.data.form.subjectId
      }).then(res => {
        res.forEach(tag => {
          tag.isCheck = false;
          if (tag.sub && tag.sub.length) {
            tag.sub.forEach(s => {
              s.isCheck = false
            })
          }
        })
        this.setData({
          goodAtList: JSON.parse(JSON.stringify(res)),
          focusList: JSON.parse(JSON.stringify(res))
        })
      })
    }
  },

  /** 选择擅长内容 start **/
  async selectGoodAt() {
    if (!this.data.form.subjectId.length) {
      wx.showToast({
        title: '请先选择科目',
        icon: 'none',
        duration: 2000
      })
      return false;
    } else if (!this.data.form.phaseId.length) {
      wx.showToast({
        title: '请先选择学段',
        icon: 'none',
        duration: 2000
      })
      return false;
    } else {
      this.getFindColumnList()
      await this.setData({showGoodAt: true})
    }
  },

  cancelSelectGoodAt() {
    this.setData({showGoodAt: false})
  },

  checkGoodAt(e) {
    const item = e.currentTarget.dataset.item
    const index = e.currentTarget.dataset.index
    item.isCheck = !item.isCheck
    this.data.goodAtList[index].isCheck = item.isCheck
    this.setData({
      goodAtList: this.data.goodAtList
    })
  },

  confirmSelectGoodAt() {
    this.data.selGoodAt = []
    this.data.form.goodAtId = []
    this.data.goodAtList.forEach(goodAt => {
      if (goodAt.isCheck) {
        this.data.selGoodAt.push(goodAt.tagsName)
        this.data.form.goodAtId.push(goodAt.tagsId)
      }
    })
    this.setData({
      showGoodAt: false,
      selGoodAt: this.data.selGoodAt,
      'form.goodAtId': this.data.form.goodAtId
    })
    console.log(this.data.focusList)
  },
  /** 选择擅长内容 end **/

  /** 选择关注内容 start **/
  async selectFocus() {
    if (!this.data.form.subjectId.length) {
      wx.showToast({
        title: '请先选择科目',
        icon: 'none',
        duration: 2000
      })
      return false;
    } else if (!this.data.form.phaseId.length) {
      wx.showToast({
        title: '请先选择学段',
        icon: 'none',
        duration: 2000
      })
      return false;
    } else {
      await this.getFindColumnList()
      this.setData({showFocus: true})
    }
  },

  cancelSelectFocus() {
    this.setData({showFocus: false})
  },

  checkFocus(e) {
    const item = e.currentTarget.dataset.item
    const index = e.currentTarget.dataset.index
    item.isCheck = !item.isCheck
    this.data.focusList[index].isCheck = item.isCheck
    this.setData({
      focusList: this.data.focusList
    })
  },

  confirmSelectFocus() {
    this.data.selFocus = []
    this.data.form.focus = []
    this.data.focusList.forEach(focus => {
      if (focus.isCheck) {
        this.data.selFocus.push(focus.tagsName)
        this.data.form.focus.push(focus.tagsId)
      }
    })
    this.setData({
      showFocus: false,
      selFocus: this.data.selFocus,
      'form.focus': this.data.form.focus
    })
  },
  /** 选择关注内容 end **/

  next() {
    function setToast(tips) {
      wx.showToast({
        title: tips,
        icon: 'none',
        duration: 2000
      })
    }
    if (!this.data.form.majorId.length) {
      setToast('请选择专业');
      return false;
    } else if (!this.data.form.subjectId.length) {
      setToast('请选择科目');
      return false;
    } else if (!this.data.form.phaseId.length) {
      setToast('请选择目标学段');
      return false;
    } else if (!this.data.form.goodAtId.length) {
      setToast('请选择擅长内容');
      return false;
    } else if (!this.data.form.focus.length) {
      setToast('请选择关注内容');
      return false;
    }
    saveNormalStudent(this.data.form).then(res => {
      wx.navigateTo({
        url: '/pages/completeInfoLast/completeInfoLast?key=student'
      })
    })
  }
})
